﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MakeItWeb.Framework;
using Telerik.Web.UI;

namespace AllNations.UI
{
    public partial class Events : MakeItWebPage, IDynamicPage
    {
        public override bool DisplayDynamicBackgroundForAdmin
        {
            get { return true; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            RadScheduler1.AppointmentDelete += new AppointmentDeleteEventHandler(RadScheduler1_AppointmentDelete);
            RadScheduler1.AppointmentInsert += new AppointmentInsertEventHandler(RadScheduler1_AppointmentInsert);
            RadScheduler1.AppointmentUpdate += new AppointmentUpdateEventHandler(RadScheduler1_AppointmentUpdate);
            RadScheduler1.ReadOnly = !IsPageAdministrator();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            RefreshCalendar();
        }

        void RadScheduler1_AppointmentUpdate(object sender, AppointmentUpdateEventArgs e)
        {
            CreateUpdateEvent(e.ModifiedAppointment);
        }

        void RadScheduler1_AppointmentInsert(object sender, SchedulerCancelEventArgs e)
        {
            CreateUpdateEvent(e.Appointment);
        }

        private void CreateUpdateEvent(Appointment app)
        {
            if (IsPageAdministrator())
            {
                StoredProcArgs args = new StoredProcArgs();
                args["pEventId"] = app.ID == null ? Guid.NewGuid() : app.ID;
                args["pHtmlPageId"] = CurrentHtmlPage.HtmlPageId;
                args["pSubjectTxt"] = app.Subject;
                args["pStartDttm"] = app.Start;
                args["pEndDttm"] = app.End;
                if (!string.IsNullOrEmpty(app.Description.Trim()))
                    args["pDescriptionTxt"] = app.Description;
                if (app.RecurrenceState != RecurrenceState.NotRecurring)
                    args["pRecurrenceSettings"] = app.RecurrenceRule;
                if (app.RecurrenceParentID != null)
                    args["pParentEventId"] = app.RecurrenceParentID;

                GenericCud.ExecuteCud("proc_cm_SaveEvent", args);

                RefreshCalendar();
            }
        }

        private void RefreshCalendar()
        {
            StoredProcArgs args = new StoredProcArgs();
            args["pHtmlPageId"] = CurrentHtmlPage.HtmlPageId;
            RadScheduler1.DataSource = GenericQry.GetDataSet("proc_cm_ListEvent", args);
            RadScheduler1.DataBind();
        }

        void RadScheduler1_AppointmentDelete(object sender, SchedulerCancelEventArgs e)
        {
            StoredProcArgs args = new StoredProcArgs();
            args["pEventId"] = e.Appointment.ID;
            GenericCud.ExecuteCud("proc_cm_DeleteEvent", args);

            RefreshCalendar();
        }

        string IDynamicPage.GetPageConfigurationControl()
        {
            return string.Empty;
        }

        string IDynamicPage.PageTypeName
        {
            get { return "Events"; }
        }
    }
}